﻿using System.Dynamic;
using System.Linq.Expressions;

namespace SemantEx
{
	public class SimpleUnaryOperationBinder : UnaryOperationBinder
	{
		public SimpleUnaryOperationBinder(ExpressionType expressionType)
			: base(expressionType)
		{
		}

		public override DynamicMetaObject FallbackUnaryOperation(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
		{
			if (!target.HasValue)
			{
				return Defer(target);
			}
			var restrictions = target.Restrictions.Merge(BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType));
			var targetExpr = target.Expression.ChangeType(target.LimitType).Convert(target.LimitType);
			return new DynamicMetaObject(ExpressionEx.EnsureObjectResult(Expression.MakeUnary(Operation, targetExpr, target.LimitType)), restrictions);
		}
	}
}